package app

import (
	"fmt"

	"github.com/urfave/cli/v2"
)

func init() {
	cli.VersionPrinter = func(c *cli.Context) {
		fmt.Println(c.App.Name, c.App.Version)
	}
	cli.VersionFlag = &cli.BoolFlag{
		Name:    "version",
		Aliases: []string{"v"},
		Usage:   "Print the version",
	}
	cli.HelpFlag = &cli.BoolFlag{
		Name:    "help",
		Aliases: []string{"h"},
		Usage:   "Show help",
	}
}

func New() *cli.App {
	app := cli.NewApp()
	app.Name = "cctl"
	app.Version = Version
	app.Description = `cctl is a command line interface for containerd`
	app.Usage = `containerd cli`
	app.DisableSliceFlagSeparator = true
	app.EnableBashCompletion = true
	app.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:    "address",
			Aliases: []string{"a"},
			Usage:   "Address for containerd's GRPC server",
			Value:   DefaultAddress,
			EnvVars: []string{"CONTAINERD_ADDRESS"},
		},
		&cli.StringFlag{
			Name:    "namespace",
			Aliases: []string{"n"},
			Usage:   "Namespace to use with commands",
			Value:   DefaultNamespace,
			EnvVars: []string{"CONTAINERD_NAMESPACE"},
		},
	}
	app.Commands = []*cli.Command{
		RunCommand,
		StopCommand,
		StartCommand,
		RmCommand,
	}
	return app
}
